<?php 
require_once 'AbstractStatementParser.php';
require_once (dirname(dirname(__FILE__)).'/viewbean/Transaction.php');

	class FNBCSVStatementParser extends AbstractStatementParser {
		public $accountId;
		
		public function loadFromString($CSVContents)
		{
			$rows = split("\n",$CSVContents);
			
			$dateFound = false;
			$this->transactions = Array();
			$count = 0;
			
			foreach($rows as $aline)
			{			
				$line =  split(",", $aline);
			
				if ($line[0]==""){
					error_log("continuing...");
					continue;
				}			
			
				if (strlen(strstr($aline,"Balance:"))>0) {
					$this->statementBalance = trim($line[1]);
					$this->availableBalance = $line[2];
				}
			
				if (strlen(strstr($aline,"Account:"))>0) {
					$this->bankAcctDetails = Array();
					$this->bankAcctDetails["BankName"] = "";
					$this->bankAcctDetails["AccountNumber"] = trim($line[1]);
					$this->bankAcctDetails["AccountType"] = "";
				}			
			
				if ($dateFound){
					
					$type = "CREDIT";
					if(strcmp(substr((double)$line[1],0,1),"-") == 0){
						$type = "DEBIT";
					}						
					
					$date = str_replace("/","-",$line[0]);
					$amount = (double)$line[1];
					$bankRef = trim($line[3]);
					$lineToHash = $line[0].$line[1].$line[2].$line[3].$this->accountId;
					$hash = trim(md5($lineToHash));
					
					$tranObj = new Transaction(null, null, $date, $amount, $bankRef, $type, null, null, null);
					$tranObj->setHash($hash);
					$this->transactions[]= $tranObj;
			
				}
				if (strlen(strstr($aline,"Date"))>0) {
					$dateFound = true;
				}
			} 
		}
	}
?>